package com.dnjn.happiness.emo.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.emo.api.model.EmoUserBean;
import com.dnjn.happiness.emo.domain.User;
import com.dnjn.happiness.system.api.domain.UserSituationVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.HashMap;
import java.util.List;

/**
 * 家庭记录Mapper接口
 *
 * @author happiness
 * @date 2023-10-13
 */
public interface UserMapper extends BaseMapper<User> {

    void updateHappinessScores(@Param("userId") Long userId, @Param("happinessScores") Integer happinessScores);

    void emptytheHappyBeans(Long userId);

    void emptytheHappyBeansToday(Long userId);

    @Select(" SELECT * FROM emo_user e LEFT JOIN sys_user u USING (user_id) WHERE e.deleted = '0' AND u.del_flag = '0' AND happiness_score IS NOT NULL AND e.user_id != 1 ")
    List<User> selectUserList();

    @Select(" select e.user_id userId,e.happiness_score happinessScore,u.nick_name nickName,u.empno empno from emo_user e LEFT JOIN sys_user u USING(user_id)  where e.deleted = '0' AND u.del_flag = '0' limit #{index},#{num} ")
    List<EmoUserBean> selectByLimit(@Param("index") int index, @Param("num") int num);

    //查询区间内需沟通人数
    @Select(" SELECT count(1) FROM emo_user e LEFT JOIN sys_user u USING(user_id) LEFT JOIN sys_dept d ON u.dept_id = d.dept_id WHERE e.deleted = '0' AND u.del_flag = '0'" +
            " AND e.happiness_score > #{startInterval} AND e.happiness_score <= #{endInterval} AND e.happiness_score < #{criticalValue} " +
            " AND ( u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set( #{deptId}, ancestors ) )  )")
    Integer selectRequiredCommunicateCount(@Param("startInterval") Integer startInterval, @Param("endInterval") Integer endInterval, @Param("criticalValue") Integer criticalValue,@Param("deptId") Long deptId);

    //查询区间内正常人数
    @Select(" SELECT count(1) FROM emo_user e LEFT JOIN sys_user u USING(user_id)  LEFT JOIN sys_dept d ON u.dept_id = d.dept_id WHERE e.deleted = '0' AND u.del_flag = '0'" +
            " AND e.happiness_score > #{startInterval} AND e.happiness_score <= #{endInterval} AND e.happiness_score >= #{criticalValue} " +
            " AND ( u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set( #{deptId}, ancestors ) )  )")
    Integer selectnormalCount(@Param("startInterval") Integer startInterval, @Param("endInterval") Integer endInterval, @Param("criticalValue") Integer criticalValue,@Param("deptId") Long deptId);

    Page<UserSituationVO> getHomePage(Page page, @Param("map") HashMap<String, Object> map);

    @Select(" SELECT DATEDIFF(create_time,NOW()),create_time FROM biz_communicate  WHERE communicate_status = '0' and deleted = '0' and user_id = #{userId} ")
    Integer selectDelayDaysByUserId(Long userId);

    @Update("update emo_user set work_date =work_date+1 where user_id = #{userId}")
    int updateWorkDate(@Param("userId") Long userId);

    void updateToZero();
}
